<?php
class GpsAction extends Action{
	
	public function index(){
		$this->display("test");
	}
	
	/**
	 * 
	 * 取得某巡检员负责的线路
	 * 请求方法：POST
	 * POST数据：Uid
	 */
	public function getLinesByUid() {
		$resArray = array(
			"isSuccess" => "",
			"Uid"       => "",
			"data"      => ""
		);
		
		$S5 = trim($_POST['Uid']);
		
		//POST过来的Uid为空时		
		if (!$S5) {
			$resArray["isSuccess"] = "N";
			$resArray["data"] = "Uid is empty";
			echo json_encode($resArray);
			die();
			unset($S5, $resArray);
		}
		
		$resArray["Uid"] = $S5;
		
		$map["S5"] = $S5;
		$ret = D("inspector")->where($map)->field("inspector_id")->select();
		unset($map);
		
		//POST过来的Uid错误时
		if (!$ret) {
			$resArray["isSuccess"] = "N";
			$resArray["data"] = "Uid is error";
			echo json_encode($resArray);
			unset($resArray, $ret);
			die();
		}
		
		$map["inspector_id"] = $ret[0]["inspector_id"];
		unset($ret,$map);
		$ret = D('Lines')->getLineByMap($map);
		
		if (!$ret) {
			$resArray["isSuccess"] = "N";
			$resArray["data"] = "the user has not line";
			echo json_encode($resArray);
			unset($resArray, $ret);
			die();
		} else {
			$resArray["isSuccess"] = "Y";
			$resArray["data"] = $ret;
			echo json_encode($resArray);
			unset($resArray,$ret);
		}
	}
	
	/**
	 * 
	 * 由线路ID取线路关键节点
	 * 请求方法POST
	 * POST数据：line_id
	 */
	public function getNodesByLineID(){
		
		$resArray = array(
			"isSuccess" => "",
			"line_id"   => "",
			"data"      => ""
		);
		
		$line_id = trim($_POST["line_id"]);
		
		//line_id为空时
		if (!trim($_POST["line_id"])) {
			$resArray["isSuccess"] = "N";
			$resArray["data"] = "line_id is empty";
			echo json_encode($resArray);
			unset($resArray);
			die();
		}
		
		$resArray["line_id"] = trim($_POST["line_id"]);
		
		$map['line_id'] = trim($_POST['line_id']);
		
		
		$ret = D('node')->where($map)->select();
		
		if (!$ret) {
			;
		}
		
		echo json_encode($ret);
		unset($map,$ret);
	}
	
	/**
	 * 
	 * 将手机端发送来的巡检位置数据保存到数据库
	 * 请求方法POST
	 * Post数据：latitude(纬度)，longitude(经度)，inspector_id(巡检员ID)，time(采集时间)
	 */
	public function sendToServ(){
		
	//   返回的基础json 	
    	$resArray = array(
    		"isSuccess" =>"",//成功为Y，失败为N
    		"data"=>"",
    	);
		
		$data ['latitude'] = trim($_POST ['latitude']);
		$data ['longitude'] = trim($_POST ['longitude']);
		$data ['inspector_id'] = trim($_POST ['inspector_id']);
		$data ['time'] = trim($_POST ['time']);
		
		//检测Post的数据是否有empty
		foreach ($data as $key => $value){
			if (empty($value)) {
				$resArray["isSuccess"] = "N";
				$resArray["data"] = $key." is_empty";
				echo json_encode($resArray);
				die();
			}
		}
		
		$model = D ( 'inspection_data' );
		$result = $model->data ( $data )->add ();
		
		//若插入数据库未成功		
		if (!$result) {
			$resArray["isSuccess"] = "N";
			$resArray["data"] = "insert Mysql fail";
			echo json_encode($resArray);
			die();
		}
		
		$resArray["isSuccess"] = "Y";
		$resArray["data"] = "";
		echo json_encode($resArray);
		
		unset($data, $model, $result, $resArray);
	}
	
	
}